{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python教程--if else\n",
    "<hr>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 单个if"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a>b\n"
     ]
    }
   ],
   "source": [
    "# 示例1.1\n",
    "a = 3\n",
    "b = 2\n",
    "if a > b:\n",
    "    print(\"a>b\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a>b且a>c\n"
     ]
    }
   ],
   "source": [
    "# 示例1.2\n",
    "a = 3\n",
    "b = 2\n",
    "c = 1\n",
    "if a > b and a > c:\n",
    "    print(\"a>b且a>c\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. if...else..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "else\n"
     ]
    }
   ],
   "source": [
    "# 示例2.1\n",
    "a = None\n",
    "if a:\n",
    "\tprint(\"if\")\n",
    "else:\n",
    "\tprint(\"else\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. if...elif..else..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "condition3 True:\n",
      "600\n"
     ]
    }
   ],
   "source": [
    "# 示例3.1\n",
    "var = 600\n",
    "if var == 200:\n",
    "\tprint(\"condition1 True:\")\n",
    "\tprint(var)\n",
    "elif var == 300:\n",
    "\tprint(\"condition2 True:\")\n",
    "\tprint(var)\n",
    "elif var >= 600:\n",
    "\tprint(\"condition3 True:\")\n",
    "\tprint(var)\n",
    "else:\n",
    "\tprint(\"else:\")\n",
    "\tprint(var)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. if...elif...else...嵌套"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1< 200 <300\n"
     ]
    }
   ],
   "source": [
    "# 示例4.1\n",
    "var = 200\n",
    "if var > 1:\n",
    "\tif var > 300:\n",
    "\t\tprint(var, \">300\")\n",
    "\telse:\n",
    "\t\tprint(\"1<\", var, \"<300\")\n",
    "else:\n",
    "\tprint(var, \"<1\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. if用于类“三目运算符”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n"
     ]
    }
   ],
   "source": [
    "# 示例5.1\n",
    "# 求a与b中的最大值\n",
    "a = 2\n",
    "b = 8\n",
    "max_num = a if a > b else b\n",
    "print(max_num)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
